#include<bits/stdc++.h>
#define sd(n) scanf("%d",&n) 
#define sld(n) scanf("%lld",&n)
#define pd(n) printf("%d", (n))
#define pld(n) printf("%lld", n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define x first
#define y second
#define INF 0x7fffffff
typedef long long int ll;
using namespace std;
//----------------------------------------------------------------------------//
typedef pair<int, int> PII;

const int N = 1010;

int n,m,k;
char g[N][N];
bool st[N][N];
PII q[N * N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

void bfs(int sx, int sy)
{
    int hh = 0, tt = 0;
    q[0] = {sx, sy};
    st[sx][sy] = true;

    int cnt=0;
    while (hh <= tt&&cnt<=k)
    {
        PII t = q[hh ++ ];

        for (int i = 0; i < 4; i ++ )
        {
        	cnt++;
            int x = t.x + dx[i], y = t.y + dy[i];
            if (x < 0 || x >= n || y < 0 || y >= n) continue;  // 出界
            //if (st[x][y]) continue;//使用过

            q[ ++ tt] = {x, y};
            st[x][y] = true;
        }
    }
}

int main()
{
    sd(n);sd(m);
    for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
    sd(k);
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < m; j ++ )
            if (g[i][j] == 'g')
            {
                bfs(i, j);
            }

    for(int i=0;i<n;i++)
    {
    	for(int j=0;j<m;j++)
    	{
    		if(st[i]) cout<<'g';
    		else cout<<'.';
    	}
    	puts("");
    }

    return 0;
}

